package com.example.onlineanswer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.onlineanswer.common.exception.BusinessException;
import com.example.onlineanswer.mapper.CommentMapper;
import com.example.onlineanswer.model.entity.Comment;
import com.example.onlineanswer.model.entity.Contest;
import com.example.onlineanswer.model.vo.CommentVO;
import com.example.onlineanswer.service.CommentService;
import com.example.onlineanswer.service.ContestService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 评论服务实现类
 */
@Service
@RequiredArgsConstructor
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {

    private final CommentMapper commentMapper;
    private final ContestService contestService;

    @Override
    public IPage<CommentVO> pageCommentsByContestId(Page<CommentVO> page, Long contestId) {
        // 查询一级评论列表
        IPage<CommentVO> commentPage = commentMapper.pageCommentsByContestId(page, contestId);
        
        // 查询每个评论的回复列表
        for (CommentVO comment : commentPage.getRecords()) {
            List<CommentVO> replies = commentMapper.getRepliesByParentId(comment.getId());
            comment.setReplies(replies);
        }
        
        return commentPage;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long addComment(Comment comment) {
        // 验证竞赛是否存在
        Contest contest = contestService.getById(comment.getContestId());
        if (contest == null) {
            throw new BusinessException("竞赛不存在");
        }
        
        // 验证用户ID是否存在
        if (comment.getUserId() == null) {
            throw new BusinessException("用户ID不能为空");
        }
        
        // 设置评论初始状态
        comment.setParentId(null); // 确保是一级评论
        comment.setLikeCount(0);
        comment.setIsDeleted(0); // 正常状态
        comment.setCreateTime(LocalDateTime.now());
        comment.setUpdateTime(LocalDateTime.now());
        
        // 保存评论
        save(comment);
        return comment.getId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long replyComment(Long contestId, Long commentId, Comment comment) {
        // 验证竞赛是否存在
        Contest contest = contestService.getById(contestId);
        if (contest == null) {
            throw new BusinessException("竞赛不存在");
        }
        
        // 验证用户ID是否存在
        if (comment.getUserId() == null) {
            throw new BusinessException("用户ID不能为空");
        }
        
        // 验证父评论是否存在
        Comment parentComment = getById(commentId);
        if (parentComment == null || parentComment.getIsDeleted() != 0) {
            throw new BusinessException("要回复的评论不存在或已被删除");
        }
        
        // 设置回复的父评论ID和竞赛ID
        comment.setContestId(contestId);
        comment.setParentId(commentId);
        comment.setLikeCount(0);
        comment.setIsDeleted(0); // 正常状态
        comment.setCreateTime(LocalDateTime.now());
        comment.setUpdateTime(LocalDateTime.now());
        
        // 保存回复
        save(comment);
        return comment.getId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean likeComment(Long commentId) {
        // 验证评论是否存在
        Comment comment = getById(commentId);
        if (comment == null || comment.getIsDeleted() != 0) {
            throw new BusinessException("评论不存在或已被删除");
        }
        
        // 点赞数+1
        int rows = commentMapper.incrementLikeCount(commentId);
        return rows > 0;
    }
} 